#include "LCD.h"

#include <wiringPi.h>

#include <cairo/cairo.h>
#include <cairo/cairo-svg.h>

#include <math.h>
#include <signal.h> //signal()
#include <stdio.h>  //printf()
#include <stdlib.h> //exit()

void LCD_test(void)
{
    // Exception handling:ctrl + c
    signal(SIGINT, Handler_2IN_LCD);

    /* Module Init */
    if (DEV_ModuleInit() != 0) {
        exit(0);
    }

    LCD_Init();

    // draw a line from (0,0) to (240,320) by cairo
    cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_RGB16_565, LCD_WIDTH, LCD_HEIGHT);
    cairo_t *cr = cairo_create(surface);
    cairo_set_source_rgb(cr, 0, 0, 0);
    cairo_paint(cr);
    cairo_set_source_rgb(cr, 1, 0, 0);
    cairo_move_to(cr, 0, 0);
    cairo_line_to(cr, LCD_WIDTH, LCD_HEIGHT);
    cairo_stroke(cr);
    cairo_set_source_rgb(cr, 0, 1, 0);
    cairo_move_to(cr, LCD_WIDTH, 0);
    cairo_line_to(cr, 0, LCD_HEIGHT);
    cairo_stroke(cr);

    // 画圆
    cairo_set_source_rgb(cr, 0, 0, 1);
    cairo_move_to(cr, LCD_WIDTH / 2 + 100, LCD_HEIGHT / 2);
    cairo_arc(cr, LCD_WIDTH / 2, LCD_HEIGHT / 2, 100, 0, 2 * 3.14159265358979323846);

    cairo_stroke(cr);
    cairo_destroy(cr);

    // convert cairo image to LCD image
    unsigned char *image = cairo_image_surface_get_data(surface);

    // 需要将大端序转换为小端序
    for (int i = 0; i < LCD_WIDTH * LCD_HEIGHT * 2; i += 2) {
        unsigned char temp = image[i];
        image[i] = image[i + 1];
        image[i + 1] = temp;
    }

    LCD_Display((unsigned char *)image);
}

int main(int argc, char *argv[])
{
    LCD_test();
    return 0;
}
